package com.yhyvr.api.timer;/**
 * Created by zhangqin on 2017/6/21.
 */

import com.lz.mybatis.jdbc.auto.dao.BoProxyMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyOrderMapper;
import com.lz.mybatis.jdbc.auto.model.BoProxyOrder;
import com.lz.mybatis.jdbc.auto.model.BoProxyOrderExample;
import com.lz.tool.ListUtils;
import com.yhyvr.service.ProxyOrderService;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 订单处理定时器
 *
 * @author
 * @create 2017-06-21 17:54
 **/
public class OrderTimer {
    @Resource
    private BoProxyOrderMapper boProxyOrderMapper;
    @Resource
    private ProxyOrderService proxyOrderService;

    public void  cancelOrder(){
        BoProxyOrderExample boProxyOrderExample = new BoProxyOrderExample();
        boProxyOrderExample.createCriteria().
                andOrderTypeEqualTo(2).
                andOrderPayStateEqualTo(1)
                .andOrderDeliveryStateEqualTo(1)
                .andOrderLockStateEqualTo(0);
        List<BoProxyOrder> boProxyOrders = boProxyOrderMapper.selectByExample(boProxyOrderExample);
        if(ListUtils.isNotBlank(boProxyOrders)){
            for (BoProxyOrder boProxyOrder : boProxyOrders) {
                Date shippingDate = boProxyOrder.getOrderDeliveryTime();
                if(shippingDate==null){
                    boProxyOrder.setOrderDeliveryTime(new Date());
                    boProxyOrderMapper.updateByPrimaryKey(boProxyOrder);
                }else {
                    Calendar calendar = Calendar.getInstance();  //得到日历
                    calendar.setTime(shippingDate);//把当前时间赋给日历
                    calendar.add(Calendar.DAY_OF_MONTH, 7);  //设置为7天前
                    Date after7days = calendar.getTime();   //得到7天前的时间
                    if (after7days.getTime() > new Date().getTime()) {
                        if (boProxyOrder.getOrderLockState() == 0) {
                            boProxyOrder.setOrderLockState(3);
                            boProxyOrderMapper.updateByPrimaryKey(boProxyOrder);
                            proxyOrderService.takeOverBySys(boProxyOrder.getOrderId());
                        }
                    }
                }
            }
        }

    }
}
